<?php
//*******************************************************
// created on 07/04/2003, created by S.Gnanavel
// Purpose:
//	fetches the privileges of this user with
// 	different companies
// Output Format:
//	privilege[company_id][privilege_desc]=permission
//*******************************************************/

function getPrivilege($user_id)
	{
	global $db;
	$db->query=generateSelectQuery("member_user_relation",array("member_id"=>"member_id","user_type_id"=>"user_type_id"),array("user_id"=>"$user_id"));
	$db->runQuery();
	
	$arrays=$db->returnArrays();
	for ($i=0;$i<count($arrays);$i++)
		{
		$user_type_id=$arrays[$i]["user_type_id"];
		$company_id=$arrays[$i]["member_id"];
		$db->query="select privilege_desc,permission from master_privilege mp, user_type_privilege utp
				where mp.privilege_id=utp.privilege_id and user_type_id='$user_type_id';";
		$db->runQuery();
		$arrays1=$db->returnArrays();
		for ($j=0;$j<count($arrays1);$j++)
			{
			$privilege_desc=$arrays1[$j]["privilege_desc"];
			$privilege[$company_id][$privilege_desc]=$arrays1[$j]["permission"];
			}
		}
	return $privilege;
	}

function getPreferences($type="default",$value="")
	{
	global $db;

	switch ($type)
		{
		case "default":
			$column_array=array("preference_desc"=>"Preference Name","default_values"=>"Values");
			$db->query=generateSelectQuery("master_preferences",$column_array);
			break;
		case "member":
			$db->query="select preference_desc as \"Preference Name\",
				preferred_values as \"Values\"
				from member_preferences cp,master_preferences mp
				where member_id=$value and
				cp.preference_id=mp.preference_id;";
			break;
		case "user":
			$db->query="select preference_desc as \"Preference Name\",
					preferred_values as \"Values\"
					from user_preferences up,master_preferences mp
					where user_id=$value and
					up.preference_id=mp.preference_id;";
			break;
		}
	$db->runQuery();
	$preference_array=$db->returnArrays();
	if($preference_array)
		{
		for($i=0;$i<count($preference_array);$i++)
			{
			$name=$preference_array[$i]["Preference Name"];
			$value=$preference_array[$i]["Values"];
			$preferences[$name]=$value;
			}
		return $preferences;
		}
	}

function getUserId($login_name)
	{
	global $db;
	$login_name=strtolower($login_name);
	$db->query="select user_id from master_user where lower(login_name) = '$login_name';";
	$db->runQuery();
	$user_array=$db->returnArrays();
	return $user_array[0]["user_id"];
	}

function getMemberId($user_id)
	{
	global $db;
	$db->query="select mur.member_id from member_user_relation mur, master_user mu where mu.primary_relation=mur.mu_relation_id and mu.user_id=$user_id;";
	$db->runQuery();
	$member_array=$db->returnArrays();
	return $member_array[0]["member_id"];
	}

function getUserTypeId($user_id,$member_id)
	{
	global $db;
	$db->query="select user_type_id from member_user_relation where user_id=$user_id and member_id=$member_id;";
	$db->runQuery();
	$member_user_details=$db->returnArrays();
	return $member_user_details[0]["user_type_id"];
	}
/*
Function Name: getUserInfo

Action: Fetches the details of an user

Created on : 29/09/2003

Author: S.Gnanavel
*/
function getUserInfo($column_name,$value)
	{
	/*class "cDatabase" should be instantiated*/
	global $db;
		
	/*Get the details of user from master_user against the information provided*/
	switch ($column_name)
		{
		case "user_id":
			$condition="and mu.user_id='$value'";
			break;
		case "login_name":
			$condition="and mu.login_name='$value'";
			break;
		}
		
	$db->query="select 
					mu.user_id, mu.login_name, mu.email_address, mur.member_id, mm.company_name,mur.user_type_id
				from 
					master_user mu, master_member mm, member_user_relation mur
				where 
					mur.member_id=mm.member_id and 
					mu.primary_relation=mur.mu_relation_id $condition;";
	$db->runQuery();
	$user_info_array["user_info"]=$db->returnArrays();
	$user_id=$user_info_array["user_info"][0]["user_id"];
	$member_id=$user_info_array["user_info"][0]["member_id"];
	
	if($user_id)
		{	
		// get the user type id's as array for the logged in user
		$db->query="select mur.member_id,mur.user_type_id from member_user_relation mur where mur.user_id=$user_id;";
		$db->runQuery();
		$user_type_id_array=$db->returnArrays();
		for($i=0;$i<count($user_type_id_array);$i++)
			{
			$user_member_id=$user_type_id_array[$i]["member_id"];
			$user_type_id=$user_type_id_array[$i]["user_type_id"];
			$user_type_details[$user_member_id][]=$user_type_id;
			}
		$user_info_array["user_type_id_array"]=$user_type_details;
		
		$db->query="select 
						product_category_id, activity_id 
					from
						member_product_details mpd, master_member mm 
					where
						mm.primary_product=mpd.product_details_id and 
						mm.member_id=$member_id;";
		$db->runQuery();
		$primary_product_details_array=$db->returnArrays();
		$user_info_array["primary_product_id"]=$primary_product_details_array[0]["product_category_id"];
		$user_info_array["primary_activity_id"]=$primary_product_details_array[0]["activity_id"];

		if($member_id > 1000)	
			{
			$user_info_array["privilege"]=getPrivilege($user_id);
			}

			// get the preferences for the user
		$default_preferences=getPreferences("default");
		$member_preferences=getPreferences("member",$member_id);
		$user_preferences=getPreferences("user",$user_id);
		if (empty($member_preferences))
			$member_preferences=array();
		if (empty($default_preferences))
			$default_preferences=array();
		if (empty($user_preferences))
			$user_preferences=array();
		$user_info_array["user_preferences"]=array_merge($default_preferences,$member_preferences,$user_preferences);
		}	
	return $user_info_array;
	}

/*
Function Name: addUser

Action: Adds an user to the database

Created on: 29/09/2003

Author: S.Gnanavel
*/		
function addUser($user_details_array)
	{
	/*Instantiate cDatabase class*/
	global $db;
	
	/*insert the following details of the user in master_user*/
	$user_details["login_name"]=strtolower($user_details_array["login_name"]);
	$user_details["password"]=base64_encode(trim($user_details_array["password"]));
	$user_details["email_address"]=$_POST["email_address"];

	$db->query="begin work;";
	$db->runQuery();

	$db->query=generateInsertQuery("master_user",$user_details);
	$db->runQuery();

	/*if successfully inserted in master_user then get the user_id for the given login_name*/
	$user_id=getUserId($user_details_array["login_name"]);

	/*insert the following details into member_user_relation*/
	$member_user_details["user_id"]=$user_id;
	$member_user_details["member_id"]=$user_details_array["member_id"];
	$member_user_details["user_type_id"]=$user_details_array["user_type_id"];

	$db->query=generateInsertQuery("member_user_relation",$member_user_details);
	$db->runQuery();

	if($db->result)
		{
		/*if successfully inserted in member_user_relation then get the primary relation*/
		$db->query="select mu_relation_id from member_user_relation order by mu_relation_id desc limit 1;";
		$db->runQuery();
		$mur_array=$db->returnArrays();
		$mu_relation_id=$mur_array[0]["mu_relation_id"];

		/*Update users primary relation with the member in master user*/
		$db->query="update master_user set primary_relation=$mu_relation_id where user_id=$user_id;";
		$db->runQuery();

// 		/*Put an entry for this user in forum_users*/
// 		$db->query=generateInsertQuery("forum_users",array("user_id"=>"$user_id"));
// 		$db->runQuery();

		/*Put an entry for this user in user_info*/
		$db->query=generateInsertQuery("user_info",array("user_id"=>"$user_id"));
		$db->runQuery();
		$db->query="commit;";
		$db->runQuery();
		}

	if(!$db->result)
		{
		$add_user["error"]="Database error. Please try again later...";
		}
	/*return NULL if registration is success or return the corressponding error*/
	return $add_user;
	}
	
function addExistingUser($user_details)
	{
	global $db;

	$db->query="begin work;";
	$db->runQuery();

	$db->query=generateInsertQuery("member_user_relation",$user_details);
	$db->runQuery();

	$db->query="select mu_relation_id from member_user_relation order by mu_relation_id desc limit 1;";
	$db->runQuery();
	$mu_relation_array=$db->returnArrays();
	$mu_relation_id=$mu_relation_array[0]["mu_relation_id"];

	$user_id=$user_details["user_id"];
	$db->query="update master_user set primary_relation=$mu_relation_id where user_id=$user_id;";
	$db->runQuery();
	
	$db->query="commit;";
	$db->runQuery();

	}	

	
function addForumUser($user_id)
	{
	global $db;
	
	$user_details_array["user_id"]=$user_id;
	$user_details_array["member_id"]=1000;
	$user_details_array["user_type_id"]="ind";
	$db->query=generateInsertQuery("member_user_relation",$user_details_array);
	$db->runQuery();
	}
	
// A function to generate random alphanumeric passwords in PHP
// It expects to be passed a desired password length, but it
// none is passed the default is set to 8 (you can change this)
function generatePassword($length = 10)
	{
	// This variable contains the list of allowable characters
   	// for the password.  Note that the number 0 and the letter
   	// 'O' have been removed to avoid confusion between the two.
   	// The same is true of 'I' and 1
   	$allowable_characters ='abcdefghefghijklmnopqrstuvwxyz0123456789_';

   	// We see how many characters are in the allowable list
   	$ps_len = strlen($allowable_characters);

   	// Max index of the characters allowed to stand and end the output.
   	$max_endpoint_ind = 25;

   	// 0-based index of the last char of the output
   	$last_char = $length - 1;

   	// Seed the random number generator with the microtime stamp
   	// (current UNIX timestamp, but in microseconds)
   	mt_srand((double)microtime() * 1000000);

   	// Declare the password as a blank string.
   	$pass = "";

   	// Loop the number of times specified by $length
   	for($i = 0; $i < $length; $i++)
		{
		// Each iteration, pick a random character from the
		// allowable string and append it to the password.
		switch ($i)
			{
			case 0:
			case $last_char:
				$pass .= $allowable_characters{mt_rand(0,$max_endpoint_ind)};
				break;
			default:
				$pass .= $allowable_characters{mt_rand(0, $ps_len)};
			}
		}
	// Retun the password we've selected
   	return $pass;
	}

function getKarma()
	{
	global $db;
	if($_SESSION[guser_id])
		{
		$db->query=generateSelectQuery("forum_users",array("karma"=>"karma"),array("user_id"=>"$_SESSION[guser_id]"));
		$db->runQuery();
		$karma_array=$db->returnArrays();
		}
	return $karma_array[0]["karma"];
	}

function getMessageCount()
	{
	global $db;
	if($_SESSION[guser_id])
		{
		$db->query=generateSelectQuery("message_details",array("message_id"=>"message_id"),array("msg_to"=>"$_SESSION[guser_id]","is_read"=>"f","is_active"=>"t"));
		$db->runQuery();
		$msg_count_array=$db->returnArrays();
		}
	return count($msg_count_array);
	}

function redirect($path="../")
	{
	if(!$_SESSION["gmember_id"])
		{
		header("Location:$path"."index.php");
		}
	else
		{
		switch($_SESSION["gmember_id"])
			{
			case 100002:
				header("Location:$path"."admin/homepage.php");
				break;

			case 1000:
				header("Location:$path"."discussion/community_home.php");
				break;

			case $_SESSION["gmember_id"] > 1000:
				header("Location:$path"."members/homepage.php");
				break;
			}
		}
	}
?>